---
title: "Python Hands-on Projects"
description: "Consolidate your Python skills and integrate what you've learned by building real-world projects."
---

## 1. Introduction

### From Theory to Practice

You have already learned the fundamental syntax of Python, as well as core knowledge in web development, data processing, and more. Now, it's time to apply this knowledge to real-world projects. Through hands-on practice, you will gain a deeper understanding of Python's power and acquire valuable development experience.

This module will provide several hands-on projects in different areas. Each project will comprehensively use the skills learned in previous modules and provide a comparative thinking process with JavaScript/Node.js implementations.

> 💡 **Learning Strategy**: Choose a project that interests you the most and start with it. Try to complete it independently. When you encounter problems, review the previous modules or consult the official documentation. The key is to "do" rather than just "watch."

## 2. Project 1: URL Shortener Service

### Project Description

Build a fully functional URL shortener service, similar to `bit.ly`. Users can submit a long URL, and the service will return a short URL. When the short URL is accessed, it should redirect to the original long URL.

**Core Features**

*   Build API endpoints using FastAPI.
*   Use Pydantic for data validation.
*   Use SQLite or another database to store URL mappings.
*   Generate unique short URL identifiers.
*   Implement redirection logic.

<PythonEditor title="URL Shortener Service: FastAPI Implementation Idea" compare={true}>
```javascript !! js
// JavaScript (Express + nanoid)
const express = require('express');
const { nanoid } = require('nanoid');

const app = express();
app.use(express.json());

const urlDatabase = {}; // Simple in-memory storage

app.post('/shorten', (req, res) => {
  const { longUrl } = req.body;
  const shortId = nanoid(7);
  urlDatabase[shortId] = longUrl;
  res.json({ shortUrl: `http://localhost:3000/${shortId}` });
});

app.get('/:shortId', (req, res) => {
  const longUrl = urlDatabase[req.params.shortId];
  if (longUrl) {
    res.redirect(longUrl);
  } else {
    res.status(404).send('Not Found');
  }
});

app.listen(3000);
```

```python !! py
# Python (FastAPI + SQLite)
import secrets
import sqlite3
from fastapi import FastAPI, HTTPException, Depends
from pydantic import BaseModel
from starlette.responses import RedirectResponse

# -- Database Setup --
DATABASE_URL = "urls.db"

def get_db():
    conn = sqlite3.connect(DATABASE_URL)
    try:
        yield conn
    finally:
        conn.close()

# -- Pydantic Models --
class URLBase(BaseModel):
    target_url: str

class URLInfo(URLBase):
    is_active: bool
    clicks: int

# -- FastAPI Application --
app = FastAPI()

@app.post("/shorten")
def create_url(url: URLBase, db: sqlite3.Connection = Depends(get_db)):
    short_id = secrets.token_urlsafe(5)
    # ... (save short_id and url.target_url to the database)
    return {"short_url": f"http://localhost:8000/{short_id}"}

@app.get("/{short_id}")
def forward_to_target_url(short_id: str, db: sqlite3.Connection = Depends(get_db)):
    # ... (find long_url from the database)
    # ... (update click count)
    # return RedirectResponse(long_url)
    pass
```
</PythonEditor>

## 3. Project 2: Command-Line Weather App

### Project Description

Create a command-line tool where a user can enter a city name, and the program will fetch and display the current weather information from a weather API (e.g., OpenWeatherMap).

**Core Features**

*   Use `argparse` or `typer` to parse command-line arguments.
*   Use `httpx` or `requests` to call a third-party API.
*   Handle the API response (JSON).
*   Format and display the weather information in the terminal.

<PythonEditor title="Command-Line Weather App: Python Implementation Idea" compare={true}>
```javascript !! js
// JavaScript (Node.js + axios + yargs)
const axios = require('axios');
const yargs = require('yargs/yargs');
const { hideBin } = require('yargs/helpers');

const argv = yargs(hideBin(process.argv)).argv;

async function getWeather(city) {
  try {
    const apiKey = 'YOUR_API_KEY';
    const url = `https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${apiKey}`;
    const response = await axios.get(url);
    console.log(`Weather in ${city}:`);
    console.log(`- Temp: ${response.data.main.temp}°K`);
    console.log(`- Description: ${response.data.weather[0].description}`);
  } catch (error) {
    console.error('Error fetching weather data');
  }
}

if (argv.city) {
  getWeather(argv.city);
}
```

```python !! py
# Python (Typer + httpx)
import typer
import httpx

API_KEY = "YOUR_API_KEY"

def main(city: str):
    """Fetches and displays the weather information for a given city."""
    url = f"https://api.openweathermap.org/data/2.5/weather?q={city}&appid={API_KEY}"
    try:
        with httpx.Client() as client:
            response = client.get(url)
            response.raise_for_status()
            data = response.json()
            print(f"Weather in {city}:")
            print(f"- Temp: {data['main']['temp']}°K")
            print(f"- Description: {data['weather'][0]['description']}")
    except httpx.HTTPStatusError as e:
        print(f"Error: Unable to fetch weather data for {city}. {e.response.status_code}")
    except Exception as e:
        print(f"An error occurred: {e}")

if __name__ == "__main__":
    typer.run(main)

# Run: python weather_app.py London
```
</PythonEditor>

## 4. Project 3: Data Analysis and Visualization Report

### Project Description

Extract and analyze data from a CSV dataset (e.g., on sales, movies, or any topic you are interested in), and generate a simple HTML report containing key metrics and charts.

**Core Features**

*   Use `pandas` to read and clean the CSV data.
*   Perform data aggregation and analysis (e.g., calculate averages, sums, groupings).
*   Use `matplotlib` or `seaborn` to create charts (e.g., bar charts, line charts).
*   Save the charts as images.
*   Use the `jinja2` template engine to generate an HTML report.

<PythonEditor title="Data Analysis Report: Python Implementation Idea" compare={true}>
```javascript !! js
// JavaScript (Node.js + danfo.js + chart.js)
// This process is relatively cumbersome in Node.js and usually requires the cooperation of multiple libraries,
// and chart generation is usually done on the client side.
// Pseudocode idea:
// 1. Use danfo.js or a similar library to read the CSV.
// 2. Perform data operations.
// 3. Pass the data to a template engine (e.g., EJS).
// 4. In the template, use a Chart.js script to render the chart.
```

```python !! py
# Python (Pandas + Matplotlib + Jinja2)
import pandas as pd
import matplotlib.pyplot as plt
from jinja2 import Environment, FileSystemLoader

# 1. Data Analysis
df = pd.read_csv("sales_data.csv")
total_sales = df["sales"].sum()
top_products = df.groupby("product")["sales"].sum().nlargest(5)

# 2. Create Chart
plt.figure()
top_products.plot(kind="bar")
plt.title("Top 5 Products by Sales")
plt.savefig("top_products.png")

# 3. Generate HTML Report
env = Environment(loader=FileSystemLoader('.'))
template = env.get_template("report_template.html")

html_content = template.render(
    total_sales=total_sales,
    top_products_table=top_products.to_html()
)

with open("report.html", "w") as f:
    f.write(html_content)

# report_template.html (Jinja2 template)
# <!DOCTYPE html>
# <html>
# <head><title>Sales Report</title></head>
# <body>
#   <h1>Sales Report</h1>
#   <p>Total Sales: {{ total_sales }}</p>
#   <h2>Top Products</h2>
#   {{ top_products_table|safe }}
#   <img src="top_products.png">
# </body>
# </html>
```
</PythonEditor>

## 5. Summary

After completing these projects, you will not just be someone who "knows" Python, but someone who can "use" Python to solve real problems. Your skills will be greatly enhanced, and you will have a deeper understanding of the Python ecosystem.

Don't stop here! Try to expand these projects, add new features, or find your own project ideas. The best way to learn programming is to constantly create and practice.
